package com.shengde.jinwawaforleader.model.personalCenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputFilter;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.model.myJob.instance_message.Constant;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProGetIdCardInfo;
import com.shengde.jinwawaforleader.network.protocolbean.ProShowClickGood;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * 修改宝宝名称 ,民族, 出生地址,户口地址
 * Created by Administrator on 2016/11/15 0015.
 */
public class EditBabyNameActivity extends BaseActivity {
	private static final String TAG = "EditBabyNameActivity";
	private static final int ADD_ID_SUCCESS = 1;
	private EditText et_add;
	private TextView tv_back;
	private TextView tv_title;
	private TextView fill_add;
	private String type;

	private String Id;
	private String cid;
	private String myType;  //这个type是为了区分是添加老师ID卡(1) 还是学生ID卡(2)
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case ADD_ID_SUCCESS:
					setResult(Constant.SETTING_ID_CARD_NUM_SAVE_SUCCESS);
					finish();
					break;
			}
		}
	};

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.edit_baby_name_activity);
	}

	@Override
	public void initView() {
		et_add = (EditText) findViewById(R.id.et_add);
		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		fill_add = (TextView) findViewById(R.id.fill_add);
	}


	@Override
	public void initData() {
		type = getIntent().getStringExtra("relative_iv_baby");
		myType = getIntent().getStringExtra("myType");
		Id = getIntent().getStringExtra("Id");
		cid = getIntent().getStringExtra("id");
		Log.i(TAG, "initData: tyoe" + type);
		if ("1".equals(type)) {
			tv_title.setText(getString(R.string.setting_nickname));
		} else if ("2".equals(type)) {
			tv_title.setText(getString(R.string.personal_sign));
		} else if ("3".equals(type)) {
			tv_title.setText(getString(R.string.setting_class_name));
		} else if ("4".equals(type)) {
			tv_title.setText(getString(R.string.setting_address));
		} else if ("5".equals(type)) {
			tv_title.setText(getString(R.string.setting_manager));
		} else if ("6".equals(type) || "13".equals(type)) {
			tv_title.setText(getString(R.string.setting_link_telephone));
		} else if ("7".equals(type)) {
			tv_title.setText(getString(R.string.setting_remark));
		} else if ("8".equals(type) || "12".equals(type) || "17".equals(type)) {
			tv_title.setText(getString(R.string.setting_name));
		} else if ("9".equals(type)) {
			tv_title.setText(getString(R.string.setting_account));
		} else if ("10".equals(type)) {
			tv_title.setText(getString(R.string.setting_password));
		} else if ("11".equals(type) || "24".equals(type)) {
			tv_title.setText(getString(R.string.setting_remark));
		} else if ("14".equals(type)) {
			tv_title.setText(getString(R.string.setting_gradute_school));
		} else if ("15".equals(type)) {
			tv_title.setText(getString(R.string.setting_frofession_name));
		} else if ("16".equals(type)) {
			tv_title.setText(getString(R.string.setting_job_name));
		} else if ("18".equals(type)) {
			tv_title.setText(getString(R.string.setting_nation));
		} else if ("19".equals(type)) {
			tv_title.setText(getString(R.string.setting_register_address));
		} else if ("20".equals(type)) {
			tv_title.setText(getString(R.string.setting_home_address));
		} else if ("21".equals(type)) {
			tv_title.setText(getString(R.string.setting_guardian_name));
		} else if ("22".equals(type)) {
			tv_title.setText(getString(R.string.setting_guardian_telephone));
		} else if ("23".equals(type)) {
			tv_title.setText(getString(R.string.setting_work_address));
		} else if ("25".equals(type) || "26".equals(type)) {
			tv_title.setText(getString(R.string.setting_id_card_num));
		}


		fill_add.setText(getString(R.string.finsh));

	}

	@Override
	public void initListener() {
		tv_back.setOnClickListener(this);
		fill_add.setOnClickListener(this);
	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.fill_add:
				Log.i(TAG, "WidgetClick: contact" + et_add.getText().toString().trim());
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				if ("1".equals(type)) {
					bundle.putString("name", et_add.getText().toString().trim());
					intent.putExtras(bundle);
					setResult(Constant.SETTING_PERSON_NAME_SAVE_SUCCESS, intent);
					finish();
				} else if ("2".equals(type)) {
					bundle.putString("personal_sign", et_add.getText().toString().trim());
					intent.putExtras(bundle);
					setResult(Constant.SETTING_PERSONAL_SIGN_SAVE_SUCCESS, intent);
					finish();
				} else if ("3".equals(type)) {
					bundle.putString("person_name", et_add.getText().toString().trim());
					intent.putExtras(bundle);
					setResult(Constant.SETTING_PERSONAL_NAME_ONE, intent);
					finish();
				} else if ("4".equals(type)) {
					bundle.putString("address", et_add.getText().toString().trim());
					intent.putExtras(bundle);
					setResult(Constant.SETTING_PERSONAL_ADDRESS, intent);
					finish();
				} else if ("5".equals(type)) {
					bundle.putString("manager", et_add.getText().toString().trim());
					intent.putExtras(bundle);
					setResult(Constant.SETTING_PERSONAL_MANAGER, intent);
					finish();
				} else if ("6".equals(type) || "13".equals(type)) {
					bundle.putString("link_telephone", et_add.getText().toString().trim());
					intent.putExtras(bundle);
					setResult(Constant.SETTING_PERSONAL_LINK_TELEPHONE, intent);
					finish();
				} else if ("7".equals(type)) {
					bundle.putString("remark_one", et_add.getText().toString().trim());
					intent.putExtras(bundle);
					setResult(Constant.SETTING_PERSONAL_REMARE_ONE, intent);
					finish();
				} else if ("8".equals(type) || "12".equals(type) || "17".equals(type)) {
					bundle.putString("name_one", et_add.getText().toString().trim());
					intent.putExtras(bundle);
					setResult(Constant.SETTING_PERSONAL_NAME_TWO, intent);
					finish();
				} else if ("9".equals(type)) {
					bundle.putString("account", et_add.getText().toString().trim());
					intent.putExtras(bundle);
					setResult(Constant.SETTING_PERSONAL_ACCOUNT, intent);
					finish();
				} else if ("10".equals(type)) {
					bundle.putString("password", et_add.getText().toString().trim());
					intent.putExtras(bundle);
					setResult(Constant.SETTING_PERSONAL_PASSWORD, intent);
					finish();
				} else if ("11".equals(type) || "24".equals(type)) {
					bundle.putString("remark_two", et_add.getText().toString().trim());
					intent.putExtras(bundle);
					setResult(Constant.SETTING_PERSONAL_REMARE_TWO, intent);
					finish();
				} else if ("14".equals(type)) {
					bundle.putString("gradute_school", et_add.getText().toString().trim());
					intent.putExtras(bundle);
					setResult(Constant.SETTING_GRADUTE_SCHOOL_SAVE_SUCCESS, intent);
					finish();
				} else if ("15".equals(type)) {
					bundle.putString("frofession_name", et_add.getText().toString().trim());
					intent.putExtras(bundle);
					setResult(Constant.SETTING_FROFRESSION_NAME_SAVE_SUCCESS, intent);
					finish();
				} else if ("16".equals(type)) {
					bundle.putString("job_name", et_add.getText().toString().trim());
					intent.putExtras(bundle);
					setResult(Constant.SETTING_JOB_NAME_SAVE_SUCCESS, intent);
					finish();
				} else if ("18".equals(type)) {
					bundle.putString("nation", et_add.getText().toString().trim());
					intent.putExtras(bundle);
					setResult(Constant.SETTING_NATION_SAVE_SUCCESS, intent);
					finish();
				} else if ("19".equals(type)) {
					bundle.putString("registe_address", et_add.getText().toString().trim());
					intent.putExtras(bundle);
					setResult(Constant.SETTING_REGISTE_ADDRESS_SAVE_SUCCESS, intent);
					finish();
				} else if ("20".equals(type)) {
					bundle.putString("home_address", et_add.getText().toString().trim());
					intent.putExtras(bundle);
					setResult(Constant.SETTING_HOME_ADDRESS_SAVE_SUCCESS, intent);
					finish();
				} else if ("21".equals(type)) {
					bundle.putString("guardian_name", et_add.getText().toString().trim());
					intent.putExtras(bundle);
					setResult(Constant.SETTING_GURADIAN_NAME_SAVE_SUCCESS, intent);
					finish();
				} else if ("22".equals(type)) {
					bundle.putString("guardian_telephone", et_add.getText().toString().trim());
					intent.putExtras(bundle);
					setResult(Constant.SETTING_GURADIAN_TELEPHONE_SAVE_SUCCESS, intent);
					finish();
				} else if ("23".equals(type)) {
					bundle.putString("work_address", et_add.getText().toString().trim());
					intent.putExtras(bundle);
					setResult(Constant.SETTING_WORK_ADDRESS_SAVE_SUCCESS, intent);
					finish();
				} else if ("25".equals(type)) {
					if (et_add.getText().toString().trim().length()!=10){
						Toast.makeText(this, "卡号不为10位,请重新输入", Toast.LENGTH_SHORT).show();
						return;
					}
					addIdCardNum(Id);

				} else if ("26".equals(type)) {
					if (et_add.getText().toString().trim().length()!=10){
						Toast.makeText(this, "卡号不为10位,请重新输入", Toast.LENGTH_SHORT).show();
						return;
					}
					changeIdCardNum(Id);
				}
				break;
			case R.id.tv_back:
				finish();
				break;
		}
	}

	/**
	 * 修改卡号
	 *
	 * @param mIdCard
	 */
	private void changeIdCardNum(String mIdCard) {

		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/director/carChange";
		} else {
			url = BaseProtocol.HOST_BASE + "/director/carChange";
		}
		RequestParams params = BaseProtocol.getRequestParams(url);
		params.addParameter("type", "2");
		params.addParameter("mycar", et_add.getText().toString().trim());
		params.addParameter("cid", cid);
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProShowClickGood proGetIdCardInfo = new Gson().fromJson(result, ProShowClickGood.class);
				if (proGetIdCardInfo != null && proGetIdCardInfo.getCode() == 0) {
					mHandler.sendEmptyMessage(ADD_ID_SUCCESS);
					Toast.makeText(EditBabyNameActivity.this, getString(R.string.change_id_success), Toast.LENGTH_SHORT).show();
				} else if (proGetIdCardInfo.getCode() == 1) {
					Toast.makeText(EditBabyNameActivity.this, getString(R.string.id_card_already_exist), Toast.LENGTH_SHORT).show();
				} else if (proGetIdCardInfo.getCode() == -1)
					Toast.makeText(EditBabyNameActivity.this, getString(R.string.change_id_fail), Toast.LENGTH_SHORT).show();

			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {

			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {

			}
		});
	}

	/**
	 * 添加卡号
	 *
	 * @param mIdCard
	 */
	private void addIdCardNum(String mIdCard) {
		et_add.setMaxEms(10);
		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/director/carChange";
		} else {
			url = BaseProtocol.HOST_BASE + "/director/carChange";
		}
		RequestParams params = BaseProtocol.getRequestParams(url);
		params.addParameter("type", "1");
		params.addParameter("mycar", et_add.getText().toString().trim());
		params.addParameter("mytype", myType);
		params.addParameter("myid", mIdCard);
		params.addParameter("schoolid", User.getSchoolId(this));
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProShowClickGood proGetIdCardInfo = new Gson().fromJson(result, ProShowClickGood.class);
				if (proGetIdCardInfo != null && proGetIdCardInfo.getCode() == 0) {
					mHandler.sendEmptyMessage(ADD_ID_SUCCESS);
					Toast.makeText(EditBabyNameActivity.this, getString(R.string.add_success), Toast.LENGTH_SHORT).show();
				} else if (proGetIdCardInfo.getCode() == 1) {
					Toast.makeText(EditBabyNameActivity.this, getString(R.string.id_card_already_exist), Toast.LENGTH_SHORT).show();
				} else if (proGetIdCardInfo.getCode() == -1)
					Toast.makeText(EditBabyNameActivity.this, getString(R.string.add_fail), Toast.LENGTH_SHORT).show();

			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {

			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {

			}
		});
	}

}
